<template>
  <div>
    <!--    搜索表单-->
    <div style="margin-bottom: 20px">
      <el-input style="width: 240px" placeholder="请输入名称" v-model="params.name"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="initCategory"><i class="el-icon-search"></i> 搜索</el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh-right"></i> 重置
      </el-button>
    </div>

    <el-table :data="tableData" stripe  row-key="id" default-expand-all>
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="success" @click="handleAdd(scope.row.id)" v-if="!scope.row.pid">添加二级分类</el-button>
          <el-button type="primary" @click="$router.push('/editCategory?id=' + scope.row.id)">编辑</el-button>
                  <el-popconfirm
                      style="margin-left: 5px"
                      title="您确定删除这行数据吗？"
                      @confirm="del(scope.row.id)"
                  >
                    <el-button type="danger" slot="reference">删除</el-button>
                  </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加二级分类" :visible="dialogFormVisible" width="40%" >
      <el-form :model="form" style="width: 600px" :rules="rules" ref="dialogForm">
        <el-form-item label="名字"  prop="name">
          <el-input v-model="form.name" ></el-input>
        </el-form-item>
        <el-form-item label="备注"  prop="remark">
          <el-input v-model="form.remark" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <!--    分页-->
    <div style="margin-top: 20px">
      <el-pagination
          background
          :curren-page="params.pageNum"
          :page-size="params.pageSize"
          layout="total, prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'Category',
  components: {},
  data() {
    return {
      rules:{
        name: [
          {required: true, message: '请输入名字', trigger: 'blur'},
        ]
      },
      form:{},
      dialogFormVisible:false,
      tableData: [],
      total: 0,
      params: {
        pageSize: 10,
        pageNum: 1,
        name: ''
       }
    }
  },
  created() {
    this.initCategory()
  },
  methods: {
    save(){
      this.$refs['dialogForm'].validate((valid) => {
        if(valid){
          request.post("/category/save",this.form).then(res=>{
            if(res.code=='200'){
              this.$notify.success("添加二级分类成功");
              this.dialogFormVisible=false
              this.$refs['dialogForm'].resetFields()
              this.initCategory()
            }else{
              this.$notify.error(res.msg)
            }
          })
        }
      })
    },
    handleAdd(id){
      this.form.pid=id
      this.dialogFormVisible=true
    },
    initCategory() {
      request.get("/category/page", {
        params: this.params
      }).then(res => {
        if (res.code == '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageSize: 10,
        pageNum: 1,
        name: '',
        phone: ''
      }
      this.initCategory()
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.initCategory()
    },
    del(id) {
      request.delete("/category/"+id).then(res=>{
        if(res.code=='200'){
          this.$notify.success("删除成功")
          this.initCategory()
        }
      })
    }
  }
}
</script>

<style scoped>

</style>